import React, { useRef } from 'react';
import { BaseHelp } from '../../../functionalComponent';
import { compHoc, getGlobalConfig, TypeOmit, iup, useRefCallback } from '../../../util';
import { getHelpBeforeOpen } from '../common';
import { HelpContent } from './HelpContent';
import { IProjectEngineeringHelp } from './interface';

/**
 * 工程项目帮助
 * @returns
 */
export const ProjectEngineeringHelp = compHoc<IProjectEngineeringHelp>((props) => {
  const cacheHelpInfo = useRef({ data: null, p: null });
  const {
    valueField = 'projCode',
    labelField = 'projectName',
    showTree = true,
    displayField,
    userCodeField,
    title,
    multiple = false,
    modalProps,
    footer,
    params: treeParams,
    treeRequest,
    beforeRequest: beforeTreeRequest,
    projectEngineeringRequest,
    projectEngineeringParams = {},
    columns,
    onBeforeOpen,
    treeProps,
    ...others
  } = props;
  const helpId = 'projectEngineering';
  const otherProps: any = others;
  otherProps.onBeforeOpen = useRefCallback(async () => {
    const [status, helpInfo] = await getHelpBeforeOpen({ onBeforeOpen, cacheHelpInfo });
    if (iup.isObject(helpInfo)) {
      return helpInfo;
    }
    return status;
  });
  otherProps.request = useRefCallback(
    async ({ pageIndex, pageSize, keyword, treeNodes, containLowOrgUser = false }) => {
      const data: any = {
        ...projectEngineeringParams,
        containLowOrgUser,
        engineeringProjectFlg: true,
        pageNum: pageIndex,
        pageSize
      };
      // 搜搜索框
      keyword && (data.query = keyword);
      showTree && (data.orgNo = treeNodes?.[0]?.code);
      if (projectEngineeringRequest) {
        return await projectEngineeringRequest(data);
      }
      const res = await iup.request.body({
        url: '/basedata/project/getProjectPages',
        data
      });
      if (res?.code === 0) return res?.data;
      return { list: [], total: 0 };
    }
  );
  otherProps.valueField = valueField;
  otherProps.userCodeField = userCodeField;
  otherProps.labelField = labelField || displayField;
  // 自定义modal弹出窗需要使用的参数
  otherProps.contentParams = {
    footer,
    helpId,
    showTree,
    multiple,
    valueField,
    labelField: otherProps.labelField,
    treeProps: {
      params: treeParams,
      beforeRequest: beforeTreeRequest,
      treeRequest,
      ...treeProps
    },
    columns: columns ?? [
      { dataIndex: 'projCode', title: '项目编码', width: multiple ? 90 : 120 },
      { dataIndex: 'projectName', title: '项目名称', tooltip: true },
      { dataIndex: 'belongOrgName', title: '所属组织', tooltip: true },
      { dataIndex: 'projManager', title: '项目经理', tooltip: true },
      { dataIndex: 'projAddress', title: '项目地址', tooltip: true },

      { dataIndex: 'projScaleName', title: '项目规模', tooltip: true },
      { dataIndex: 'engineImportanceCateName', title: '工程重要性类别', tooltip: true },
      { dataIndex: 'engineCateName', title: '工程类别', tooltip: true }
    ]
  };
  if (title) {
    otherProps.contentParams.helpTitle = title;
  }
  if (onBeforeOpen) {
    otherProps.cache = false;
  }

  const { width, height } = getGlobalConfig().default.helpConfig;
  return (
    <BaseHelp
      {...otherProps}
      modal
      helpContent={HelpContent}
      multiple={multiple}
      modalProps={{ width, height, ...modalProps }}
    />
  );
}, 'ProjectEngineeringHelp');

/**
 * 核算项目帮助-多选
 * @returns
 */
export const MultipleProjectEngineeringHelp = React.forwardRef<any, TypeOmit<IProjectEngineeringHelp, 'multiple'>>(
  (props, ref) => {
    return <ProjectEngineeringHelp {...props} ref={ref} multiple />;
  }
);

export default ProjectEngineeringHelp;
